package cn.hiapi.user.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.system.ILocationService;
import cn.hiapi.interfaces.system.data.LocationData;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.entity.shop.ShopScore;
import cn.hiapi.shop.entity.shop.ShopType;
import cn.hiapi.shop.service.shop.ShopScoreService;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.service.shop.ShopTypeService;
import cn.hiapi.shopkeeper.entity.user.SubscribeShop;
import cn.hiapi.shopkeeper.service.user.SubscribeShopService;
import cn.hiapi.user.query.HistoryRecordQuery;
import cn.hiapi.user.vo.SubscribeShopVo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author AdinZ
 * @date 2022/12/31 22:28
 */
@RestController
@RequestMapping("/user/subscribe")
public class SubscribeShopController extends BasicQueryController<SubscribeShop, Long, SubscribeShopVo, HistoryRecordQuery> {

    private final SubscribeShopService basicService;

    protected SubscribeShopController(SubscribeShopService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
        this.basicService = basicService;
    }


    @ApiOperation(value = "订阅-取消订阅", notes = "订阅-取消订阅", nickname = "AdinZ")
    @PostMapping("")
    public ResponseEntity subscribe(@RequestParam("shopId") long id) {
        Token<Object> token = TokenGet.get();
        List<SubscribeShop> list = basicService.findList(new QueryWrapper().eq("uid", token.getFid()).eq("shopId", id));

        JSONObject data = new JSONObject();

        if (list.isEmpty()) {
            SubscribeShop subscribe = new SubscribeShop();
            subscribe.setShopId(id);
            subscribe.setUid(token.getFid());
            basicService.save(subscribe);
            data.put("status", 1);


        } else {
            this.basicService.deleteByUid(token.getFid(), id);
            data.put("status", 0);
        }
        return toSuccess(data);
    }


    @ApiOperation(value = "订阅状态", notes = "订阅状态", nickname = "AdinZ")
    @GetMapping("")
    public ResponseEntity getSubscribe(@RequestParam("shopId") long id) {
        Token<Object> token = TokenGet.get();
        List<SubscribeShop> list = basicService.findList(new QueryWrapper().eq("uid", token.getFid()).eq("shopId", id));
        JSONObject data = new JSONObject();
        data.put("status", list.isEmpty() ? 0 : 1);
        return toSuccess(data);
    }


    @Override
    public HistoryRecordQuery setQuery(HistoryRecordQuery query) {
        query.setUid(TokenGet.get().getFid());
        query.setDirection(Sort.Direction.DESC);
        query.setProperties("id");
        return super.setQuery(query);
    }

    @Override
    public List<SubscribeShopVo> toListVo(List<SubscribeShop> list, String[] fields) {
        return super.toList(list, fields);
    }

    @Override
    public SubscribeShopVo toData(SubscribeShop data, String[] fields) {
        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        SubscribeShopVo vo = shop.copy(new SubscribeShopVo());
        vo.setSubscribeId(data.getId());
        StringBuffer address = new StringBuffer();
        List<LocationData> locationDataList = this.dispatchContext.getServiceOne(ILocationService.class).locationGet(shop.getStateId(), shop.getCityId(), shop.getDistrictId());
        locationDataList.forEach(e -> address.append(e.getName()).append(" "));
        address.append(shop.getAddress());
        vo.setAddress(address.toString());
        ShopScore score = this.dispatchContext.getServiceOne(ShopScoreService.class).getByShopId(0, shop.getId());
        if (score != null)
            vo.setScore(score.getComplexScore());
        ShopType type = this.dispatchContext.getServiceOne(ShopTypeService.class).get(shop.getCid());
        if (type != null)
            vo.setCategory(type.getName());

        return vo;
    }
}
